from rest_framework.viewsets import GenericViewSet
from django.conf import settings
from utils.common_mixins import APIListModelMixin
from utils.decorators import wrap_cache
from .models import Banner
from .serializers import BannerSerializer


class BannerView(GenericViewSet, APIListModelMixin):
    # 过滤所有被删除或不被上架的轮播图 并按照排序等级排序 数字越低，优先级越高
    queryset = Banner.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    serializer_class = BannerSerializer
    # 缓存存放的键
    cache_key = 'banner_list'

    @wrap_cache  # 缓存装饰器
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)

